<?php

namespace backend\modules\pooai\controllers;

use backend\modules\base\controllers\BaseController;
use backend\modules\pooai\models\ZpooaiPages;
use Yii;
use backend\modules\pooai\models\ZpooaiGallImages;
use backend\modules\pooai\models\search\ZpooaiGallImagesSearch;
use yii\base\Exception;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * ZpooaigallImagesController implements the CRUD actions for ZpooaiGallImages model.
 */
class ZpooaigallimagesController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all ZpooaiGallImages models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ZpooaiGallImagesSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->getBodyParams());

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single ZpooaiGallImages model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new ZpooaiGallImages model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new ZpooaiGallImages();
        $zpooaiPages = ZpooaiPages::find()->all();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->pooai_gall_images_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                'zpooaiPages'=>$zpooaiPages
            ]);
        }
    }

    /**
     * Updates an existing ZpooaiGallImages model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->pooai_gall_images_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing ZpooaiGallImages model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the ZpooaiGallImages model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return ZpooaiGallImages the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ZpooaiGallImages::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    /**
     * 移动排序图片
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionSort()
    {
        $postData = Yii::$app->request->getRawBody();
        $dataArray = json_decode($postData,true);
        $ZpooaiGallImages  = new ZpooaiGallImages();
        $colNameId = $ZpooaiGallImages->primaryKey()[0];
        //
        $connection = Yii::$app->db;
        $transaction = $connection->beginTransaction();
        try {
            foreach($dataArray as $key=>$val) {
                $connection->createCommand()->update($ZpooaiGallImages->tableName(), ['sort_number' => $val['sort_number']], $colNameId.'= "'.$val['id'].'"')->execute();
            }
            $transaction->commit();
            return json_encode([true,[]]);
        } catch (Exception $e) {
            $transaction->rollBack();
            return json_encode([$e->getMessage()]);
        }
    }
}
